<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>薪酬管理</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .container {
            margin: 20px;
        }

        .table-container {
            margin-top: 20px;
            width: 100%;
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table th, table td {
            padding: 10px;
            border: 1px solid #ddd;
            text-align: left;
        }

        table th {
            background-color: #f2f2f2;
        }

        button {
            padding: 5px 10px;
            cursor: pointer;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>薪酬数据管理</h1>

    <!-- 查询所有薪酬数据 -->
    <button id="loadSalariesBtn">加载薪酬数据</button>

    <!-- 薪酬数据表格 -->
    <div class="table-container">
        <table id="salaryTable">
            <thead>
            <tr>
                <th>姓名</th>
                <th>薪酬标准ID</th>
                <th>基本工资</th>
                <th>补助资金</th>
                <th>养老保险</th>
                <th>失业保险</th>
                <th>医疗保险</th>
                <th>住房公积金</th>
                <th>奖励金</th>
                <th>扣除金</th>
                <th>个人薪酬</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <!-- 数据将通过JavaScript动态填充 -->
            </tbody>
        </table>
    </div>
</div>

<script>
    // 点击按钮加载薪酬数据
    $(document).ready(function() {
        // 点击加载按钮
        $('#loadSalariesBtn').click(function() {
            loadSalaries();
        });

        // 加载薪酬数据
        function loadSalaries() {
            $.ajax({
                url: '/salary/all',  // 后端接口地址
                method: 'GET',
                success: function(response) {
                    console.log('薪酬数据:', response);
                    var tableBody = $('#salaryTable tbody');
                    tableBody.empty();  // 清空现有数据

                    if (response && response.length > 0) {
                        response.forEach(function(salary) {
                            var row = `<tr>
                                <td>${salary.name}</td>
                                <td>${salary.salary_id}</td>
                                <td>${salary.total_salary}</td>
                                <td>${salary.allowance}</td>
                                <td>${salary.pension_insurance}</td>
                                <td>${salary.unemployment_insurance}</td>
                                <td>${salary.medical_insurance}</td>
                                <td>${salary.housing_fund}</td>
                                <td><input type="number" class="bonus" value="${salary.bonus}" data-id="${salary.id}" /></td>
                                <td><input type="number" class="dock" value="${salary.dock}" data-id="${salary.id}" /></td>
                                <td>${salary.hr_salary}</td>
                                <td>
                                    <button class="update-btn" data-id="${salary.id}">更新</button>
                                </td>
                            </tr>`;
                            tableBody.append(row);
                        });
                    } else {
                        tableBody.append('<tr><td colspan="12">没有数据</td></tr>');
                    }
                },
                error: function(xhr, status, error) {
                    alert('加载数据失败: ' + error);
                }
            });
        }

        // 更新薪酬数据
        $(document).on('click', '.update-btn', function() {
            var salaryId = $(this).data('id');
            var bonus = $(this).closest('tr').find('.bonus').val();
            var dock = $(this).closest('tr').find('.dock').val();

            var salaryData = {
                id: salaryId,
                bonus: parseFloat(bonus),
                dock: parseFloat(dock)
            };

            $.ajax({
                url: '/salary/update',
                method: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(salaryData),
                success: function(response) {
                    alert('薪酬信息更新成功');
                    loadSalaries();  // 更新后重新加载薪酬数据
                },
                error: function(xhr, status, error) {
                    alert('更新失败: ' + error);
                }
            });
        });
    });
</script>

</body>
</html>
